package com.jdd.modules.storecar.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdd.common.system.query.QueryGenerator;
import com.jdd.common.util.excel.entity.params.ExportParams;
import com.jdd.common.util.excel.web.entity.vo.NormalExcelConstants;
import com.jdd.common.util.excel.web.view.EasypoiSingleExcelView;
import com.jdd.modules.parkcar.util.ParkCarUtil;
import com.jdd.modules.storecar.entity.StoreCarList;
import com.jdd.modules.storecar.service.IStoreCarListService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

/**
 * @Description: TODO
 * @author: scott
 * @date: 2021年04月30日 17:59
 */
@Slf4j
@Api(tags = "储值车")
@RestController
@RequestMapping("/storeCar/storeCarList")
public class StoreCarListController {

    @Resource
    private ExecutorService executorService;

    private ParkCarUtil parkCarUtil;

    @Resource
    private IStoreCarListService storeCarListService;

    /**
     * 储值车导出excel
     *
     * @param request
     * @param response
     */

    @RequestMapping(value = "/storeExportXls")
    public ModelAndView storeExportXls(HttpServletRequest request, HttpServletResponse response, StoreCarList storeCarList) {
        // Step.1 组装查询条件
        QueryWrapper<StoreCarList> queryWrapper = QueryGenerator.initQueryWrapper(storeCarList, request.getParameterMap());
        // queryWrapper.and(wrapper -> wrapper.eq("carTypeId",storeCarList.));
        parkCarUtil = new ParkCarUtil();
        queryWrapper.eq("license_type", 3);
        // Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new EasypoiSingleExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "储值车列表");
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("储值车列表数据", "导出信息"));
        // 导出文件名称
        // 查询表中数据大小
        int listSize = storeCarListService.count(queryWrapper);
        // 定义每页数量
        int pageSize = 100;
        // 定义线程列表
        List<CompletableFuture<ModelAndView>> futureList = new ArrayList<>();
        // 循环查询，每次查询一页
        for (int pageNo = 1; pageNo <= listSize / pageSize + 1; pageNo++) {
            int number = pageNo;
            // 开启线程查询
            CompletableFuture<ModelAndView> completableFuture = CompletableFuture.supplyAsync(() -> {
                return parkCarUtil.addDataToModelAndViewStoreCar(new ModelAndView(), storeCarListService, queryWrapper, number, pageSize);
            }, executorService);
            futureList.add(completableFuture);
        }
        // 等待每个线程结束后
        CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0])).join();
        List<StoreCarList> resultList = new ArrayList<>();
        futureList.forEach(a -> {
            try {
                // 将结果添加到列表中
                resultList.addAll((List<StoreCarList>) a.join().getModel().get(NormalExcelConstants.DATA_LIST));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        // 将数据添加到Excel文件内
        mv.addObject(NormalExcelConstants.CLASS, StoreCarList.class);
        mv.addObject(NormalExcelConstants.DATA_LIST, resultList);

        return mv;
    }
}
